<template>
  <div
    class="player"
    :style="`
      left: ${r * 50}px;
      top: ${r * 50}px;
      transform: translate(${player.x * 50}px, ${player.y * 50}px);
    `"
  >
    <div class="playerImg" :class="player.direction" />
    <div
      class="playerSlot"
      :class="weapon && weapon.followPlayer ? player.direction : ''"
    >
      <slot></slot>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { isEmpty } from "@/util/common.js";
import { getWeaponById } from "@/util/weapon.js";

export default {
  computed: {
    ...mapState("map", ["player", "system"]),
    r() {
      return (this.system.areaBlockDiameter - 1) / 2;
    },
    weapon() {
      if (isEmpty(this.player.inventory, this.player.currentInventoryIndex)) {
        return null;
      }
      let id = this.player.inventory[this.player.currentInventoryIndex].id;
      return getWeaponById(id);
    },
  },
  mounted() {
    let allowMove = true;
    window.addEventListener("keydown", (e) => {
      e.preventDefault();

      if (e.keyCode === 9) {
        // TAB
        this.$store.state.map.player.showBag = !this.player.showBag;
        return;
      }

      if (this.player.showBag) return;

      if (!allowMove) return;
      this.$store.dispatch("map/playerMove", e.keyCode);
      // 更新周围区块
      this.$bus.$emit("updateAreaBlock", this.player.x, this.player.y);
      allowMove = false;
      setTimeout(() => {
        allowMove = true;
      }, 1000 / this.player.speed);
    });

    // 初始更新周围区块
    this.$nextTick(() => {
      this.$bus.$emit("updateAreaBlock", this.player.x, this.player.y);
    });
  },
};
</script>

<style scoped>
.player {
  box-sizing: border-box;
  position: absolute;
  width: 50px;
  height: 50px;
  padding: 9px;
  transition: transform linear 150ms;
}
.playerSlot {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 1;
  transform-origin: center center;
}
.playerSlot.right {
  transform: rotate(0deg);
}
.playerSlot.bottom {
  transform: rotate(90deg);
}
.playerSlot.left {
  transform: rotate(180deg);
}
.playerSlot.top {
  transform: rotate(270deg);
}
.playerImg {
  position: relative;
  width: 32px;
  height: 32px;
  background-image: url("~@/assets/image/player_skin_1.png");
  background-repeat: no-repeat;
  background-position: -32px -32px;
  background-size: 96px 128px;
  animation: 500ms steps(1) infinite;
}
.playerImg.bottom {
  animation-name: playerMoveBottom;
}
.playerImg.left {
  animation-name: playerMoveLeft;
}
.playerImg.right {
  animation-name: playerMoveRight;
}
.playerImg.top {
  animation-name: playerMoveTop;
}

@keyframes playerMoveBottom {
  0% {
    background-position: calc(-32px * 0) calc(-32px * 0);
  }
  33% {
    background-position: calc(-32px * 1) calc(-32px * 0);
  }
  67% {
    background-position: calc(-32px * 2) calc(-32px * 0);
  }
  100% {
    background-position: calc(-32px * 3) calc(-32px * 0);
  }
}

@keyframes playerMoveLeft {
  0% {
    background-position: calc(-32px * 0) calc(-32px * 1);
  }
  33% {
    background-position: calc(-32px * 1) calc(-32px * 1);
  }
  67% {
    background-position: calc(-32px * 2) calc(-32px * 1);
  }
  100% {
    background-position: calc(-32px * 3) calc(-32px * 1);
  }
}

@keyframes playerMoveRight {
  0% {
    background-position: calc(-32px * 0) calc(-32px * 2);
  }
  33% {
    background-position: calc(-32px * 1) calc(-32px * 2);
  }
  67% {
    background-position: calc(-32px * 2) calc(-32px * 2);
  }
  100% {
    background-position: calc(-32px * 3) calc(-32px * 2);
  }
}

@keyframes playerMoveTop {
  0% {
    background-position: calc(-32px * 0) calc(-32px * 3);
  }
  33% {
    background-position: calc(-32px * 1) calc(-32px * 3);
  }
  67% {
    background-position: calc(-32px * 2) calc(-32px * 3);
  }
  100% {
    background-position: calc(-32px * 3) calc(-32px * 3);
  }
}
</style>